package com.appspot.yttv.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appspot.yttv.client.model.StationSource;
import com.appspot.yttv.client.model.VideoCategory;
import com.google.gwt.http.client.URL;

/**
 * Generate Url for feeds
 * 
 * @author Ramona Bobohalma
 */
public class FeedUrlProvider {
	
	private static final String GDATA = "http://gdata.youtube.com/feeds/api/";
	private static final String STANDARD_FEEDS = "standardfeeds/"; 
	private static final String USERS = "users/";
	private static final String PLAYLISTS = "playlists/";
	private static final String VIDEOS = "videos/";
	private static final String UPLOADS = "uploads/";
	
	private static final String COMMON_PARAMETERS = "max-results=50&v=2&alt=json-in-script&callback=";
	
		
	private static final Map<StandardFeed, String> STANDARD_FEED_TO_URI = 
		new HashMap<StandardFeed, String>() {
		private static final long serialVersionUID = -7330409784160400068L;

		{
			put(StandardFeed.RISING_VIDEOS, "top_favorites");
			put(StandardFeed.MOST_POPULAR, "most_popular");
			put(StandardFeed.MOST_VIEWED, "most_viewed");
			put(StandardFeed.FEATURED, "recently_featured");
		}
	};
	
	public static enum StandardFeed {
		RISING_VIDEOS, MOST_POPULAR, MOST_VIEWED, FEATURED;
	}
	
	public static String getFeedUrl(StandardFeed feed, VideoCategory category, String parameters) {
		return GDATA + STANDARD_FEEDS + STANDARD_FEED_TO_URI.get(feed) 
			+ "_" + category.getTerm() + parameters;
	}
	
	public static List<String> forVideoCategory( VideoCategory category) {
		List<String> sourceUrls = new ArrayList<String>();
		
		for(FeedUrlProvider.StandardFeed feed : FeedUrlProvider.StandardFeed.values()) {
			if (feed != FeedUrlProvider.StandardFeed.FEATURED) {
				sourceUrls.add(FeedUrlProvider.getFeedUrl(feed,category,
						"?time=today&v=2&alt=json-in-script&callback="));
			} else {
				sourceUrls.add(FeedUrlProvider.getFeedUrl(feed,category,
						"?v=2&alt=json-in-script&callback="));
			}
		}
		return sourceUrls;
	}
	
	public static List<String> forChannel(String name) {
		List<String> sourceUrls = new ArrayList<String>();
		sourceUrls.add(GDATA + USERS + name +"/" + UPLOADS + "?" + COMMON_PARAMETERS);
		return sourceUrls;
	}
	
	public static List<String> forPlaylist(String id) {
		List<String> sourceUrls = new ArrayList<String>();
		sourceUrls.add(GDATA + PLAYLISTS + id + "?" + COMMON_PARAMETERS);
		return sourceUrls;
	}
	
	public static List<String> forSearch(String query) {
		List<String> sourceUrls = new ArrayList<String>();
		sourceUrls.add(GDATA + VIDEOS + "?q=" + URL.encode(query) + "&" + COMMON_PARAMETERS);
		return sourceUrls;
	}
	
	public static List<String> forStationSource(StationSource source) {
		if (source.getType() == StationSource.Type.CATEGORY) {
			return forVideoCategory(new VideoCategory(source.getContentId(), source.getContentId()));
		} else if (source.getType() == StationSource.Type.CHANNEL) {
			return forChannel(source.getContentId());
		} else if (source.getType() == StationSource.Type.PLAYLIST) {
			return forPlaylist(source.getContentId());
		} else if (source.getType() == StationSource.Type.SEARCH) {
			return forSearch(source.getContentId());
		}
		return null;
	}
}
